package com.albd.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Environment;
import android.util.Log;

public class FileUtil
{
	private static final int BYTE_BUFFER_SIZE = 256;
	public static final String APP_NAME = "name";
	public static final String ACTION = "action";
	public static final String TIME = "time";
	public static final String ALLOWED = "allowed";
	public static final String NUMBER = "number";
	public static final String CONTENT = "content";

	public static void createFileMark(int aPid)
	{
		File file = new File("/data/tyu_" + aPid);
		if(!file.exists())
		{
			try
			{
				file.createNewFile();
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	public static boolean hasFileMark(int aPid)
	{
		File file = new File("/data/tyu_" + aPid);
		boolean res = false;
		if(file.exists())
		{
			Log.w(file.getAbsoluteFile().toString(), "has marked");
			res = true;
		}
		else
		{
			Log.w(file.getAbsoluteFile().toString(), "has no mark");
			createFileMark(aPid);
		}
		return res;
	}

	public static byte[] readFile(String file)
	{
		ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(BYTE_BUFFER_SIZE);
		byte[] buffer = new byte[BYTE_BUFFER_SIZE];
		FileInputStream is = null;
		try
		{
			is = new FileInputStream(file);
			int len = -1;
			while ((len = is.read(buffer)) > 0)
			{
				byteBuffer.write(buffer, 0, len);
			}
			is.close();
		}
		catch (Exception e)
		{
			return null;
		}
		return byteBuffer.toByteArray();
	}

	public static String readFile(String file, char endBit)
	{
		byte[] b = readFile(file);
		if(b == null) { return null; }
		for (int i = 0; i < b.length; i++)
		{
			if(endBit == b[i]) { return new String(b, 0, i); }
		}
		return new String(b);
	}
	
	public static String getJsonString(String appName, String action, boolean isAllowed)
	{	
		JSONObject jsonObj = new JSONObject();

		try
		{
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");     
            String time = sDateFormat.format(new java.util.Date());  
            
			jsonObj.put(APP_NAME, appName);
			jsonObj.put(TIME, time);
			jsonObj.put(ACTION, action);
			jsonObj.put(ALLOWED, isAllowed ? "允许" : "拒绝");
		}
		catch (JSONException e)
		{
			e.printStackTrace();
		}

		return jsonObj.toString();
	}
	
	public static String getJsonString(String appName, String action, boolean isAllowed, String number, String content)
	{	
		JSONObject jsonObj = new JSONObject();

		try
		{
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");     
            String time = sDateFormat.format(new java.util.Date());
            
			jsonObj.put(APP_NAME, appName);
			jsonObj.put(TIME, time);
			jsonObj.put(ACTION, action);
			jsonObj.put(ALLOWED, isAllowed ? "允许" : "拒绝");
			jsonObj.put(NUMBER, number);
			jsonObj.put(CONTENT, content);
		}
		catch (JSONException e)
		{
			e.printStackTrace();
		}

		return jsonObj.toString();
	}
	
	public static void SaveInterceptInfo(String stringToWrite) {
		if (Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState())) {
			String foldername = Environment.getExternalStorageDirectory()
					.getPath();

			File folder = new File(foldername);
			if (folder == null || !folder.exists()) {
				folder.mkdir();
			}

			String fileName = "/Interception" + ".json";
			File targetFile = new File(foldername + fileName);
			OutputStreamWriter osw;
			
			try {
				if (!targetFile.exists()) {
					targetFile.createNewFile();
					osw = new OutputStreamWriter(new FileOutputStream(
							targetFile,true), "utf-8");
					osw.write(stringToWrite);
					osw.close();
				} else {
					osw = new OutputStreamWriter(new FileOutputStream(
							targetFile,true), "utf-8");
					osw.write("\n" + stringToWrite);
					osw.flush();
					osw.close();
				}
			} catch (Exception e) {
				// Toast.makeText(this,e.toString(),Toast.LENGTH_LONG).show();
			}
		} else {
			// Toast.makeText(this,"未发现SD卡！",Toast.LENGTH_LONG).show();
		}
	}
}
